<?php

/**
 * user actions.
 *
 * @package    skiaplanche.ca
 * @subpackage user
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class userActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->sap_users = Doctrine::getTable('sap_user')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new sap_userForm();
  }

  public function executeNewAccount( sfWebRequest $request ) {
      $this->form = new createAccountForm();
      $this->form->getCSRFToken(); 
  }

  public function executeCreateAccount( sfWebRequest $request ) {
      $this->forward404Unless( $request->isMethod( sfRequest::POST ) );
      $this->form = new createAccountForm();
      $this->processCreateAccountForm( $request, $this->form );
      $this->setTemplate( 'newAccount' );
  }

  private function processCreateAccountForm( sfWebRequest $request, sfForm $form) {
    $form->bind( $request->getParameter( $form->getName() ) );
    if( $request->getAttribute( 'password' ) == $request->getAttribute( 'password_check' ) ) {
        if( $form->isValid() ) {
            $user = $form->save();
            $this->redirect( 'user/edit?id=' . $user->getId() );
        }
    } else {
        echo 'error !';
    }
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new sap_userForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($sap_user = Doctrine::getTable('sap_user')->find(array($request->getParameter('id'))), sprintf('Object sap_user does not exist (%s).', $request->getParameter('id')));
    $this->form = new sap_userForm($sap_user);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($sap_user = Doctrine::getTable('sap_user')->find(array($request->getParameter('id'))), sprintf('Object sap_user does not exist (%s).', $request->getParameter('id')));
    $this->form = new sap_userForm($sap_user);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($sap_user = Doctrine::getTable('sap_user')->find(array($request->getParameter('id'))), sprintf('Object sap_user does not exist (%s).', $request->getParameter('id')));
    $sap_user->delete();

    $this->redirect('user/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $sap_user = $form->save();

      $this->redirect('user/edit?id='.$sap_user->getId());
    }
  }
}
